package com.biz.config;

import org.neo4j.driver.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.boot.autoconfigure.domain.*;
import org.springframework.context.annotation.*;
import org.springframework.data.neo4j.core.transaction.*;
import org.springframework.data.neo4j.repository.config.*;
import org.springframework.data.transaction.*;
import org.springframework.jdbc.datasource.*;
import org.springframework.transaction.*;
import org.springframework.transaction.annotation.*;

import javax.sql.*;

/**
 * @Description 配置neo4j 和 mysql 事务
 *             1. @EnableNeo4jRepositories 用于扫描指定包下的repository
 *             2. @EntityScan 用于扫描 neo4j实体
 * @Author liuxi
 * @Date 2024/7/14
 *
 **/

@Configuration
@EnableTransactionManagement
@EnableNeo4jRepositories(basePackages = {"com.graph.repository"})
@EntityScan(basePackages = {"com.graph.model.neo4j"})
public class Neo4jConfig {


    /**
     * 此处为了修改默认事务，必须改
     * 加载了Neo4J依赖库之后，transactionManager变成Neo4jTransactionManager
     */
    @Bean("transactionManager")
    @Primary
    public DataSourceTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }


    /**
     * Neo4J的事务管理
     */
    @Bean("neo4jTransactionManager")
    public Neo4jTransactionManager neo4jTransactionManager(Driver driver) {
        return new Neo4jTransactionManager(driver);
    }

    /**
     * 需要使用多种事务时
     */
    @Autowired
    @Bean(name = "multiTransactionManager")
    public PlatformTransactionManager multiTransactionManager(Neo4jTransactionManager neo4jTransactionManager,
                                                               DataSourceTransactionManager mysqlTransactionManager) {
        return new ChainedTransactionManager(neo4jTransactionManager, mysqlTransactionManager);
    }

}
